#include "server.h"
#include "ui_server.h"
#include "mytcpserver.h"

#include <QFile>
#include <QDebug>
#include <QHostAddress>


Server::Server(QWidget *parent): QWidget(parent), ui(new Ui::Server)
{
    ui->setupUi(this);
    loadConfig();
    MyTcpServer::getInstance().listen(QHostAddress(m_strIP), m_usPORT);
}

Server::~Server()
{
    delete ui;
}

// 加载配置
void Server::loadConfig()
{
    QFile file(":/server.config");
    if(file.open(QIODevice::ReadOnly))
    {
        QByteArray  baData   = file.readAll();
        QString     strData  = QString(baData);
        QStringList strList  = strData.split("\r\n");
        this->      m_strIP  = strList.at(0);
        this->      m_usPORT = strList.at(1).toUShort();
        this-> m_strRootRath = strList.at(2);
        file.close();
        qDebug() << "loadConfig ip:" << m_strIP << "port:" << m_usPORT<< "m_strRootRath:" <<m_strRootRath;
    }
    else
    {
        qDebug() << "打开配置失败";
    }
}

Server &Server::getInstance()
{
    static Server instance;
    return instance;
}

QString Server::getRootRath()
{
    return m_strRootRath;
}
